class EntriesController < ApplicationController
  before_filter :authenticate
  
  # GET /entries
  # GET /entries.xml
  def index
    @entries = Entry.find(:all)

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @entries }
    end
  end

  # GET /entries/1
  # GET /entries/1.xml
  def show
    @entry = Entry.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @entry }
    end
  end

  # GET /entries/new
  # GET /entries/new.xml
  def new
    @entry = Entry.new
    @list = List.find(params[:list_id])

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @entry }
    end
  end

  # GET /entries/1/edit
  def edit
    @entry = Entry.find(params[:id])
  end

  # POST /entries
  # POST /entries.xml
  def create
    @entry = Entry.new(params[:entry])
    @list = List.find(params[:entry][:list_id])

    respond_to do |format|
      if @entry.save
        flash[:notice] = 'Entry was successfully created.'
        format.html { redirect_to(@entry.list) }
        format.xml  { render :xml => @entry, :status => :created, :location => @entry }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @entry.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /entries/1
  # PUT /entries/1.xml
  def update
    @entry = Entry.find(params[:id])

    respond_to do |format|
      if @entry.update_attributes(params[:entry])
        flash[:notice] = 'Entry was successfully updated.'
        format.html { redirect_to(@entry.list) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @entry.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /entries/1
  # DELETE /entries/1.xml
  def destroy
    @entry = Entry.find(params[:id])
    @entry.destroy

    respond_to do |format|
      format.html { redirect_to(url_for(@entry.list)) }
      format.xml  { head :ok }
    end
  end
  
  def read
    entry = Entry.find(params[:entry_id])
    entry.status = 2
    entry.save
    redirect_to(entry.url)
  end
  
  # Used by bookmarklet save feature
  def save
    if (params[:lid])
      list = List.find(params[:lid])
    
      entry = Entry.find(:first, :conditions => {:url => params[:u], :list_id => list.id}) || Entry.new
      entry.url = params[:u]
      entry.title = params[:t]
      entry.summary = params[:s]
      entry.list = list
      entry.status = 1
      if (entry.save)
        render :action => 'saved'
      else
        render :text => entry.errors.full_messages
      end
    else
      @lists = List.find(:all, :include => :entries).sort{|x,y| x.entries.count(:conditions => 'status = 1') <=> y.entries.count(:conditions => 'status = 1')}.reverse
    end
  end
  
  # Used by bookmarklet Next In List feature
  def next_in_list
    list = List.find(params[:lid])
    entry = list.first_entry
    if (entry)
      redirect_to "/read/#{entry.id}"
    else
      redirect_to url_for(list)
    end
  end
end